﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Dln.Handler
{
    public class EventMessage : Message
    {
        private const string EVENT = "Event";
        private const string EVENT_KEY = "EventKey";
        private const string TICKET = "Ticket";
        private const string SCANCODEINFO = "ScanCodeInfo";

        /// <summary>
        /// 事件类型
        /// </summary>
        public string Event { get; set; }
        /// <summary>
        /// 二维码的ticket，可用来换取二维码图片
        /// </summary>
        public string Ticket { get; set; }
        /// <summary>
        /// 事件KEY值，与自定义菜单接口中KEY值对应
        /// </summary>
        public string EventKey { get; set; }
        /// <summary>
        /// 事件ScanCodeInfo值 
        /// </summary>
        public ScanCodeInfo ScanCodeInfo = new ScanCodeInfo();
        /// <summary>
        /// 从xml数据加载文本消息
        /// </summary>
        /// <param name="xml"></param>
        public static EventMessage LoadFromXml(string xml)
        {
            EventMessage em = null;
            if (!string.IsNullOrEmpty(xml))
            {
                XElement element = XElement.Parse(xml);
                em = new EventMessage();
                em.FromUserName = element.Element(XMLNodeConfig.FROM_USERNAME).Value;
                em.ToUserName = element.Element(XMLNodeConfig.TO_USERNAME).Value;
                em.CreateTime = element.Element(XMLNodeConfig.CREATE_TIME).Value;
                em.Event = element.Element(EVENT) == null ? "" : element.Element(EVENT).Value;
                em.EventKey = element.Element(EVENT_KEY) == null ? "" : element.Element(EVENT_KEY).Value;
                em.Ticket = element.Element(TICKET) == null ? "" : element.Element(TICKET).Value;
                //em.ScanCodeInfo.ScanResult = element.Element(SCANCODEINFO) == null ? "" : element.Element(SCANCODEINFO).Element("ScanResult").Value;
                em.ScanCodeInfo.ScanResult = element.Element(SCANCODEINFO) == null ? "" : element.Element(SCANCODEINFO).Element("ScanResult").Value;
            }
            return em;
        }
    }
    public class ScanCodeInfo
    {
        public string ScanType { get; set; }
        public string ScanResult { get; set; }
    }
}
